// pages/login/login.js
import utils from '../../utils/util.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getUserInfo();
  },
  onShareAppMessage:function(res){
    if (res.from === 'button') {
      console.log(res.target)
    }
    return {
      title: '来博饼',
      path: '/pages/main/main'
    }
  },
  /**
   * 登录
   */
  tapLogin(e) {
    let that = this;
    let url = '/codeVerify';
    utils.login().then((code) => {
      wx.showLoading({
        title: '加载中...',
      })
      let data = {
        "code": code,
        "nickName": e.detail.userInfo.nickName,
        "headimg": e.detail.userInfo.avatarUrl,
      }
      utils.http(url, data, false).then(res => {
        if (res.code == 1) {
          console.log(res)
          that.setData({
            userInfo: res.data
          })
          wx.setStorageSync('user_login', res.data)
          wx.showToast({
            title: '登录成功'
          })
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'none'
          })
        }
      }, rej => {
        console.log(rej)
      })
    })
  },
  getRecord: function(e) {
    wx.navigateTo({
      url: '../getRecord/getRecord',
    })
  },
  getaboutUs: function(e) {
    wx.navigateTo({
      url: '../aboutUs/aboutUs',
    })
  },
  getUserInfo: function(e) {
    let that = this
    utils.auth().then(res => {
      if (res) {
        let userInfo = wx.getStorageSync('user_login')
        this.setData({
          userInfo
        })
      }
    })
  }
})